package com.example.hanqinshan.gifttalk.home.channel;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hanqinshan.gifttalk.R;
import com.example.hanqinshan.gifttalk.entity.ChannelItemData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Risky57 on 2017/6/28.
 */

public class ChannelAdapter extends BaseAdapter {

    private List<ChannelItemData.ItemEntity> data;
    private LayoutInflater mInflater;
    private Context mContext;

    public ChannelAdapter (Context context) {
        mContext = context;
        data = new ArrayList<>();
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void addAll(List<ChannelItemData.ItemEntity> data){
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount () {
        return data.size();
    }

    @Override
    public ChannelItemData.ItemEntity getItem (int position) {
        return data.get(position);
    }

    @Override
    public long getItemId (int position) {
        return 0;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_channel, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        ChannelItemData.ItemEntity item = getItem(position);
        ChannelItemData.ItemEntity.AuthorBean author = item.getAuthor();
        Glide.with(mContext).load(author.getAvatar_url()).into(holder.imgAvator);
        holder.textNickname.setText(author.getNickname());
        holder.textIntroduction.setText(author.getIntroduction());
        Glide.with(mContext).load(item.getCover_image_url()).into(holder.imgCover);
        holder.textTitle.setText(item.getTitle());
        holder.textMainIntroduction.setText(item.getIntroduction());
        holder.textMainTitle.setText(item.getColumn().getTitle());
        holder.textLikesCount.setText(String.valueOf(item.getLikes_count()));
        return convertView;
    }

    public static class ViewHolder{
        private ImageView imgAvator;
        private TextView textNickname;
        private TextView textIntroduction;
        private ImageView imgCover;
        private TextView textTitle;
        private TextView textMainIntroduction;
        private TextView textMainTitle;
        private TextView textLikesCount;

        public ViewHolder(View itemView){
            imgAvator = (ImageView) itemView.findViewById(R.id.img_avator);
            textNickname = (TextView) itemView.findViewById(R.id.text_nickname);
            textIntroduction = (TextView) itemView.findViewById(R.id.text_introduction);
            imgCover = (ImageView) itemView.findViewById(R.id.img_cover);
            textTitle = (TextView) itemView.findViewById(R.id.text_title);
            textMainIntroduction = (TextView) itemView.findViewById(R.id.text_main_introduction);
            textMainTitle = (TextView) itemView.findViewById(R.id.text_main_title);
            textLikesCount = (TextView) itemView.findViewById(R.id.text_likes_count);

        }
    }
}